<template>
  <el-container class="home-container">
 
    <!-- 头部 -->
  <el-header>
    <div>
      <span>电商后台管理系统</span>
    </div>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  
  <!-- 主体 -->
  <el-container>
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px': '200px'">
      <div class="fold" @click="toggleCollapse">
        <div v-if="!isCollapse" class="el-icon-s-fold"></div>
        <div  v-if="isCollapse" class="el-icon-s-unfold"></div>
      </div>
      <!-- 菜单栏 -->
      <el-menu background-color="#313743" text-color="#fff" active-text-color="#359cff" 
      unique-opened :collapse="isCollapse" :collapse-transition="false" :router="true" 
      :default-active="activePath">
        <!-- 一级菜单 -->
      <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
        <!-- 一级菜单的模板区域 -->
        <template slot="title">
          <!-- 图标 -->
          <i :class="ioconsObj[item.id]"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级 -->
         <el-menu-item :index="'/'+ subItem.path+''" v-for="subItem in item.children"
          :key="subItem.id" @click="saveNavState('/'+ subItem.path)">
         <!-- index作为跳转地址 path +/ -->
           <template slot="title">
          <!-- 图标 -->
          <i :class="ioconsTowObj[subItem.id]"></i>
          <!-- 文本 -->
          <span>{{subItem.authName}}</span>
        </template>
         </el-menu-item>
      </el-submenu>
    </el-menu>
    <!--  -->
    </el-aside>
    <el-main>
      <!-- 路由 -->
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data(){
    return{
      menulist:[],
     ioconsObj:{
        125:'el-icon-user-solid',
        103:'el-icon-s-grid',
        101:'el-icon-s-goods',
        102:'el-icon-s-claim', 
        145:'el-icon-s-data'
      },
      ioconsTowObj:{
        110:'el-icon-tickets',
        111:'el-icon-document-remove',
        112:'el-icon-mobile',
        104:'el-icon-shopping-cart-1',
        115:'el-icon-shopping-bag-1',
        121:'el-icon-shopping-bag-2',
        107:'el-icon-edit-outline',
        146:'el-icon-data-analysis'
      },
      isCollapse:false,
      // 未激活的链接地址
      activePath:''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
methods:{
  logout(){
    window.sessionStorage.clear()
    this.$router.push('/login')
  },
 async getMenuList(){
  const{data:res} = await this.$http.get('menus')
  if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
  this.menulist = res.data
  console.log(res);
  },
  // 点击菜单折叠
  toggleCollapse(){
this.isCollapse = ! this.isCollapse
  },
  // 保存链接的激活状态
  saveNavState(activePath) {
    window.sessionStorage.setItem('activePath',activePath)
    this.activePath = activePath
  }
}
}
</script>

<style lang="less" scoped>
.home-container {
  width: 100%;
  height: 100%;
}
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 20px;
  >div {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #313743;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #e9edf0;
}
.fold {
  background-color: #5c5c5c;
  height: 40px;
  text-align: center;
  color: white;
  .el-icon-s-unfold::before {
    line-height: 40px;
  font-size: 20px;
}
.el-icon-s-fold::before {
  line-height: 40px;
  font-size: 20px;
}
}

</style>